package org.water.garden.view;

import org.water.garden.R;

import android.content.Context;
import android.database.Cursor;
import android.provider.Im;
import android.provider.Im.Contacts;
import android.view.View;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class ContactListAdapter extends ResourceCursorAdapter {

	public ContactListAdapter(Context context, Cursor c) {
		super(context, R.layout.contact_list_row, c);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		//Contact name text view
		String nickName = cursor.getString(cursor.getColumnIndex(Contacts.NICKNAME));
		if(nickName.length() > 20){
			nickName = nickName.substring(0, 20) + "...";
		}
		((TextView)view.findViewById(android.R.id.text1)).setText(nickName);
		
		String userName = cursor.getString(cursor.getColumnIndex(Contacts.USERNAME));
		((TextView)view.findViewById(android.R.id.text2)).setText(userName);
		
		int mode = cursor.getInt(cursor.getColumnIndex(Im.Presence.PRESENCE_STATUS));
		if(mode == Im.Presence.AVAILABLE){
			((ImageView)view.findViewById(R.id.contact_icon)).setImageResource(R.drawable.msn_online);
		}else if(mode == Im.Presence.OFFLINE){
			((ImageView)view.findViewById(R.id.contact_icon)).setImageResource(R.drawable.msn_offline);
		}else {
			((ImageView)view.findViewById(R.id.contact_icon)).setImageResource(R.drawable.msn_busy);
		}
	}
}